//
//  ANShowNewsViewController.m
//  AttitudeNews
//
//  Created by 刘晓萌 on 15/11/26.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "ANShowNewsViewController.h"
#import "DataHelper.h"
#import "ANNewsModel.h"
#import "ANNewsInfoNormalModel.h"
#import "Urls.h"
#import "playViewController.h"
#import "LXM_DataDownloadTools.h"
#import "UIViewController+LLT_UIViewControllerTools.h"

@interface ANShowNewsViewController ()

@property (weak, nonatomic) IBOutlet UILabel *headTitle;
@property (strong, nonatomic) IBOutlet UIView *anShowView;
@property (weak, nonatomic) IBOutlet UIWebView *showInfoWebView;

@property (strong, nonatomic) DataHelper *dataHelper;
@property (strong, nonatomic) ANNewsInfoNormalModel *normalNews;

@end

@implementation ANShowNewsViewController

/**
 * 重写model的set方法
 */
-(void)setNewsModel:(ANNewsModel *)newsModel{
    if (_newsModel != newsModel) {
        _newsModel = nil;
        _newsModel = newsModel;
    }
}

/**
 * 设置数据显示
 */
-(void)makeData{
    
    NSString *str =[@"http://c.3g.163.com/nc/article/" stringByAppendingString:self.newsModel.docid];
    NSString *str1 =[str stringByAppendingString:@"/full.html"];
    //NSLog(@"%@",str1);
    [LXM_DataDownloadTools downloadDataWithURL:str1 andMethod:GET andBody:nil andBlock:^(NSData *data) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        self.normalNews = [[ANNewsInfoNormalModel alloc]init];
        [self.normalNews setValuesForKeysWithDictionary:dictionary[self.newsModel.docid]];
        NSString *title = self.normalNews.title;
        NSString *time = self.normalNews.ptime;
        NSString *header = [NSString stringWithFormat:@"<div class=\"title\">%@</div><div class=\"time\">%@</div>", title, time];  //height=\"200\"
        NSURL *cssURL = [[NSBundle mainBundle] URLForResource:@"news" withExtension:@"css"];
        //循环加图片
        NSArray *arr = self.normalNews.img;
        int i = 0;
        for (NSDictionary *dict in arr) {
            if (i== arr.count) {
                continue;
            }
            NSString *body = self.normalNews.body;
            NSString *imgHTML = [NSString stringWithFormat:@"<img src=\"%@\" width=\"%f\" >", dict[@"src"],self.view.frame.size.width - 20];
            //NSLog(@"%@",dict[@"ref"]);
            NSString *html = [body stringByReplacingOccurrencesOfString:dict[@"ref"] withString:imgHTML];
            self.normalNews.body = html;
            i ++; 
        }
        //循环加视频
        NSArray *videoArr = self.normalNews.video;
        for (NSDictionary *dict in videoArr) {
            NSString *body = self.normalNews.body;
            NSString *videoHTML = [NSString stringWithFormat:@"<video src=\"%@\" width=\"%f\" controls autoplay >", dict[@"url_mp4"],self.view.frame.size.width - 20];
            //NSLog(@"%@",dict[@"ref"]);
            NSString *html = [body stringByReplacingOccurrencesOfString:dict[@"ref"] withString:videoHTML];
            self.normalNews.body = html;
        }
        
        NSString *html = [NSString stringWithFormat:@"%@%@",header,self.normalNews.body];
        html = [NSString stringWithFormat:@"%@<link rel=\"stylesheet\" href=\"%@\">", html, cssURL];
        
        [self.showInfoWebView loadHTMLString:html baseURL:nil];
    }];
}

/**
 * 设置页面显示内容
 */
-(void)setupView{
    self.headTitle.text = self.newsModel.headTitle;
    self.anShowView.backgroundColor = [UIColor greenColor];
}

/**
 * 点击返回按键
 */
- (IBAction)didClickLeftButton:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.dataHelper = [[DataHelper alloc]init];
    [self setupView];
    [self makeData];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
